package com.example.demo_job.controller;


import com.example.demo_job.config.MySessionListener;
import com.example.demo_job.entity.Company;
import com.example.demo_job.entity.Position;
import com.example.demo_job.entity.Seeker;
import com.example.demo_job.pojo.User;
import com.example.demo_job.service.CompanyService;
import com.example.demo_job.utils.R;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/company")
//@CrossOrigin(origins = {"*"},methods = {RequestMethod.GET, RequestMethod.POST})
@CrossOrigin
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    //登录地址：http://localhost:8080/hqdx/company/login
    @RequestMapping("/login")
    public R login(@RequestBody User user, HttpSession httpSession){
        String cusername=user.getUsername();
        String cpassword=user.getPassword();
        Company company=companyService.findByUsername(cusername);
        if(company==null){
            //用户名不存在
            httpSession.invalidate();//销毁会话
            return R.error().message("用户名不存在");
        }
        else {
            //密码错误
            Company companyLogin=companyService.login(cusername,cpassword);
            if(companyLogin==null) {
                httpSession.invalidate();//销毁会话
                return R.error().message("密码错误");
            }
            //登录成功
            //httpsession
            String cname = company.getCname();
            Set<HttpSession> sessions = MySessionListener.getSessions();
            for (HttpSession session :sessions){
                if(cname.equals((String) session.getAttribute("currentUser"))){
                    httpSession.invalidate();//销毁会话
                    return R.error().message("该用户已在其他地方登录");
                }
            }
            httpSession.setAttribute("currentUser", cname);
            return R.ok().data("items",companyLogin);
        }
    }


    //登录地址：http://localhost:8080/hqdx/company/loginout
    @RequestMapping("/loginout")
    public R loginout(String cusername){
        Company company = companyService.findByUsername(cusername);
        MySessionListener.loginout(company.getCname());
        return R.ok().message("退出成功");
    }

    //注册地址：http://localhost:8080/hqdx/company/register
    @RequestMapping("/register")
    public R register(String cusername,
                      String cpassword,
                      String cname,
                      String cyear,
                      String caddress,
                      String cemail,
                      String cphone
                      ){
        //cinfo
        Company company=companyService.findByUsername(cusername);
        if(company!=null) return R.error().message("用户名已存在");
        int register=companyService.register(cusername,cpassword,cname,cyear,caddress,cemail,cphone);
        if(register==0) return R.error().message("注册失败");
        return R.ok().message("注册成功");
    }

    //返回公司介绍：http://localhost:8080/hqdx/company/showCompanyProfile
    @RequestMapping("/showCompanyProfile")
    public R showSeekerCv(String cinfo){
        String Profile=companyService.showCompanyProfile(cinfo);
        if(Profile==null) return R.error().message("文本为空");
        return R.ok().data("items",Profile);
    }

    //返回职位详细：http://localhost:8080/hqdx/company/showPositionDescription
    @RequestMapping("/showPositionDescription")
    public R showPositionDescription(String pinfo){
        String Description=companyService.showPositionDescription(pinfo);
        if(Description==null) return R.error().message("文本为空");
        return R.ok().data("items",Description);
    }



    //更新公司信息地址：http://localhost:8080/hqdx/company/updateComMessage
    @RequestMapping("/updateComMessage")
    public R updateComMessage(String cusername,String cname,String cyear,String caddress,String cemail,String cphone,String cv){

        try {
            companyService.updateComMessage(cusername,cname, cyear, caddress, cemail, cphone);
            reviseCCV(cusername,cv);
        }catch(Exception e){
            return R.error().message("邮箱或电话号码重复");
        }
        Company company = companyService.findByUsername(cusername);
        return R.ok().message("更改成功").data("items",company);
    }

    //推荐牛人的地址:http://localhost:8080/hqdx/company/showTenSeeker
    @RequestMapping("/showTenSeeker")
    public R showTenSeeker(){
        List<Seeker> seekerList=companyService.showTenSeeker();
        return R.ok().data("items",seekerList);
    }



    //展示公司信息地址：http://localhost:8080/hqdx/company/showComMessage
//    @RequestMapping("/showComMessage")
//    public R showComMessage(String cusername){
//    }


    @RequestMapping("/reviseCCV")
    public R reviseCCV(String cusername,String cv){
        String Tsinfo= companyService.FindCinfo(cusername);
        if(Tsinfo==null) return R.error().message("失败");
        companyService.Write(Tsinfo,cv);
        return R.ok().message("修改成功");
    }







}
